/**
 * This file is part of aion-emu <aion-emu.com>.
 *
 *  aion-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.serverpackets;

import com.aionemu.gameserver.controllers.movement.MovementMask;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.Creature.MoveData;
import com.aionemu.gameserver.model.gameobjects.moveData.PlayableMoveData;
import com.aionemu.gameserver.network.aion.AbstractAionServerPacket;
import com.aionemu.gameserver.network.aion.GameClient;

/**
 * This packet is displaying movement of players etc.
 * 
 * @author Lyahim, -Nemesiss-
 * 
 */
public class SM_MOVE extends AbstractAionServerPacket<GameClient>
{
	/**
	 * Object that is moving.
	 */
	private Creature creature;
	/**
	 * Constructs new <tt>SM_MOVE</tt> packet
	 * 
	 * @param Creature
	 */
	public SM_MOVE(Creature creature)
	{
		this.creature = creature;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void writeImpl(GameClient client)
	{
		MoveData moveData = creature.getMoveData();
		writeD(creature.getObjectId());
		writeF(creature.getX());
		writeF(creature.getY());
		writeF(creature.getZ());
		writeC(creature.getHeading());
		
		writeC(moveData.movementMask);

		if (moveData instanceof PlayableMoveData)
		{
			PlayableMoveData playermoveData = (PlayableMoveData)moveData;
			if ((moveData.movementMask & MovementMask.STARTMOVE) == MovementMask.STARTMOVE)
			{
				if ((moveData.movementMask & MovementMask.MOUSE) == 0)
				{
					writeF(playermoveData.vectorX);
					writeF(playermoveData.vectorY);
					writeF(playermoveData.vectorZ);
				}
				else
				{
					writeF(moveData.xTarget);
					writeF(moveData.yTarget);
					writeF(moveData.zTarget);
				}
			}
			if ((moveData.movementMask & MovementMask.GLIDE) == MovementMask.GLIDE)
			{
				writeC(playermoveData.glideFlag);
			}
			if ((moveData.movementMask & MovementMask.VEHICLE) == MovementMask.VEHICLE)
			{
				writeD(playermoveData.unk1);
				writeD(playermoveData.unk2);
				writeF(playermoveData.vectorX);
				writeF(playermoveData.vectorY);
				writeF(playermoveData.vectorZ);
			}
		}
		else
		{
			if ((moveData.movementMask & MovementMask.STARTMOVE) == MovementMask.STARTMOVE)
			{
				writeF(moveData.xTarget);
				writeF(moveData.yTarget);
				writeF(moveData.zTarget);
			}
		}
	}
}
